package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityPassGroupDetailBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterGroupRaceDetail
import com.gexiaobao.pigeon.ui.adapter.AdapterGroupRaceDetailHead
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import com.kingja.loadsir.core.LoadService
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat


/**
 * @Author      : hxw
 * @Date        : 2024/7/20 13:34
 * @Describe    : 团体赛详情
 */
class ActivityPassGroupDetail : BaseActivity<OrgRaceViewModel, ActivityPassGroupDetailBinding>() {

    private var projectId = 0
    private var titleGroup = ""

    private val mAdapter: AdapterGroupRaceDetail by lazy { AdapterGroupRaceDetail(arrayListOf()) }
    private var mAdapterHead: AdapterGroupRaceDetailHead? = null
    private var headList: ArrayList<String> = arrayListOf("1")
    private lateinit var loadsir: LoadService<Any>

    @SuppressLint("NotifyDataSetChanged", "MissingInflatedId")
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)

        titleGroup = intent.getStringExtra("titleGroup").toString()
        mDatabind.includeBar.tvTitle.text = titleGroup

        projectId = intent.getIntExtra("projectId", 0)

        mDatabind.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.initFooter {
                mViewModel.getAppTeamProject(false, projectId.toString())
            }
        }

        val headerView = LayoutInflater.from(this).inflate(R.layout.head_group_race_detail_head, null)
        val swipeRecyclerViewHead = headerView.findViewById<SwipeRecyclerView>(R.id.swipeRecyclerViewHead)
        mAdapter.addHeaderView(headerView)

        mAdapterHead = AdapterGroupRaceDetailHead(arrayListOf(), titleGroup)
        swipeRecyclerViewHead.init(LinearLayoutManager(this), mAdapterHead!!)
        mAdapterHead!!.setList(headList)
        mAdapterHead!!.notifyDataSetChanged()

        loadsir = loadServiceInit(mDatabind.swipeRefreshLayout) {
            //点击重试时触发的操作
            onRefresh()
        }
        onRefresh()

        mDatabind.swipeRefreshLayout.init {
            onRefresh()
        }
    }

    private fun onRefresh() {
        loadsir.showLoading()
        mViewModel.getAppTeamProject(true, projectId.toString())
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取app团体赛的详情  */
        mViewModel.appTeamResult.observe(this) {
            loadListData(it, mAdapter, loadsir, mDatabind.swipeRecyclerView, mDatabind.swipeRefreshLayout)
        }
    }

    // 方法来动态修改列标题
    private fun updateColumnTitles() {
        // 获取当前的表格数据对象
        val tableData = mDatabind.smartTable.tableData
        // 修改列标题
        tableData.childColumns[0].columnName = titleGroup
        // 刷新表格数据以应用新的列标题
        mDatabind.smartTable.notifyDataChanged()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}